package com.fastloan.mgt.product.question.controller;

import com.fastloan.mgt.product.question.model.ProductQuestion;
import com.fastloan.mgt.product.question.model.ProductQuestionOption;
import com.fastloan.mgt.product.question.service.ProductQuestionService;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;

/**
 * @author chenkai
 * createTime 2017-12-2017/12/27 18:00
 */
@RestController
@RequestMapping("/api/web/question")
public class ProductQuestionController {

    @Autowired
    private ProductQuestionService productQuestionService;


    @GetMapping
    public ResultTo queryQuestionTree() {
        return new ResultTo().setData(productQuestionService.queryQuestion());
    }

    @GetMapping("/{questionId}")
    public ResultTo queryQuestionById(@PathVariable String questionId) {
        return new ResultTo().setData(productQuestionService.queryQuestionById(questionId));
    }

    @GetMapping("/begin")
    public ResultTo queryBeginQuestion() {
        return new ResultTo().setData(productQuestionService.queryBeginQuestion());
    }

    @PostMapping
    public ResultTo insertQuestion(ProductQuestion productQuestion, String questionOptionJson, @RequestParam(required = false) String optionId) {
        try {
            productQuestionService.insertQuestion(productQuestion, questionOptionJson, optionId);
        } catch (Exception e) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, e.getMessage());
        }
        return new ResultTo();
    }

    @PutMapping
    public ResultTo updateQuestion(ProductQuestion productQuestion, String questionOptionJson, @RequestParam(required = false) String optionId) {
        try {
            productQuestionService.updateQuestion(productQuestion, questionOptionJson, optionId);
        } catch (Exception e) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, e.getMessage());
        }
        return new ResultTo();
    }

}